﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog
{
    public partial class frmTaikhoan : PRE.Catalog.FrmBase //ket noi frmBase de su them ,xoa, sua, ....ben formgiao dien
    {
        //xu ly goi du lieu tu csdl len, them ,xoa ,sua...
        DAL.Entities_DAL.TaikhoanDAL _dal = new DAL.Entities_DAL.TaikhoanDAL();
        //ket noi form BLL ma tu tang
        BLL.Entities_BLL.TaikhoanBLL _bll = new BLL.Entities_BLL.TaikhoanBLL();

        public frmTaikhoan()
        {
            InitializeComponent();
        }

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus(false);
            ResetText();
            txtTenDN.Focus();
            txtTenDN.Properties.ReadOnly = false;

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = txtTenDN.Text;

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            txtMatkhau.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        ///

        //nut refest
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            txtTenDN.Properties.ReadOnly = true;

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            base.PerformRefresh();
        }
        /// <summary>
        /// Perform when click save button
        /// </summary>
        /// 

        //nut luu
        protected override void PerformSave()
        {
            var quyen = Proper(txtQuyenTC.Text);

            if (IsAdd)
            {
                var o = new DAL.Entities.Taikhoan()
                {
                    TEN_DANG_NHAP = txtTenDN.Text,
                    MAT_KHAU = txtMatkhau.Text,
                    QUYEN_TRUY_CAP = Convert.ToInt32(quyen)
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                var _id = grvMain.GetFocusedRowCellValue("TEN_DANG_NHAP") + "";

                var o = new DAL.Entities.Taikhoan()
                {
                    TEN_DANG_NHAP = _id,
                    MAT_KHAU = txtMatkhau.Text,
                    QUYEN_TRUY_CAP = Convert.ToInt32(txtQuyenTC.Text)
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            base.PerformSave();
        }
        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txtTenDN.Text = null;
            txtMatkhau.Text = null;
            txtQuyenTC.Text = null;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        /// 

        //nut xoa
        protected override void ClearDataBindings()
        {
            txtTenDN.DataBindings.Clear();
            txtMatkhau.DataBindings.Clear();
            txtQuyenTC.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        /// 

        //show du lieu data luoi len textbox
        protected override void DataBindingControl()
        {
            txtTenDN.DataBindings.Add("EditValue", _dtb, ".TEN_DANG_NHAP");
            txtMatkhau.DataBindings.Add("EditValue", _dtb, ".MAT_KHAU");
            txtQuyenTC.DataBindings.Add("EditValue", _dtb, ".QUYEN_TRUY_CAP");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtTenDN.Properties.ReadOnly = isReadOnly;
            txtMatkhau.Properties.ReadOnly = isReadOnly;
            txtQuyenTC.Properties.ReadOnly = isReadOnly;

            dsTaikhoan.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ClearDataBindings();
            ResetText();
            ChangeStatus(false);
            ReadOnlyControl(false);

            txtTenDN.Properties.ReadOnly = false;
            txtTenDN.ResetText();
            txtTenDN.Focus();

            dsTaikhoan.Enabled = true;

            base.PerformSearch();
        }
        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            // try
            // {
            _dtb = _dal.Select();

            if (_dtb != null)
            {
                dsTaikhoan.DataSource = _dtb;
                //string _dem = _dal.Select_count();
                //label4.Text = String.Format("Số thành viên hiện tại là: {0} thành viên", _dem);
            }
            // }
            // catch
            // {
            //MessageBox.Show("Dữ liệu load chưa xong! Vui lòng thử lại sau!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            // }

            base.LoadData();
        }
        #endregion

        private void frmTaikhoan_Load(object sender, EventArgs e)
        {
            PerformRefresh();
        }

        private void dockPanel2_Click(object sender, EventArgs e)
        {

        }
    }
}
